<template>
  <div class="seller-container">
    <div class="sellerchart" ref="seller_ref"></div>
  </div>
</template>

<script>
export default {
  name: "Seller",
  data() {
    return {
      chartInstance: null,
      allData: "",
      currentPage: 1,
      totalPage: 0,
      timerId: "",
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter()
  },
  destroyed() {
    clearInterval(this.timerId);
    window.removeEventListener('resize',this.screenAdapter)
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echart.init(this.$refs.seller_ref, "customed");

        const initOption = {
        title: {
          text: "| 商家销售统计",
          textStyle: {
            fontSize: 66,
          },
          left: 20,
          top: 20,
        },
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            lineStyle: {
              width: 60,
              color: "#2d3443",
              type: "solid",
            },
            z: 0,
          },
        },
        series: [
          {
            type: "bar",
            barWidth: 60,
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "#fff",
              },
            },
            itemStyle: {
              borderRadius: [0, 30, 30, 0],
            },
          },
        ],
        color: {
          type: "linear",
          x: 0,
          y: 0,
          x2: 1,
          y2: 0,
          colorStops: [
            {
              offset: 0,
              color: "#5052ee", // 0% 处的颜色
            },
            {
              offset: 1,
              color: "#ab6ee5", // 100% 处的颜色
            },
          ],
          global: false,
        },
      };
      initOption && this.chartInstance.setOption(initOption);

      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData() {
      const { data: ret } = await this.$http.get("seller");
      this.allData = ret;
      this.allData.sort((a, b) => {
        return a.value - b.value;
      });
      this.totalPage =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);
      const sellerNames = showData.map((item) => {
        return item.name;
      });
      const sellerValve = showData.map((item) => {
        return item.value;
      });
      const dataOption = {
        yAxis: {
          data: sellerNames,
        },
        series: [
          {
            data: sellerValve,
          },
        ],
      };
      dataOption && this.chartInstance.setOption(dataOption);
    },
    startInterval() {
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },
    screenAdapter() {
      const title = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: title,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: title,
            },
          },
        },
        series: [
          {
            barWidth: title,
            
            itemStyle: {
              borderRadius: [0, title /2, title / 2, 0],
            },
          },
        ],
        
      };
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
  },
};
</script>

<style>
</style>